package com.example.demo.controller;


import com.example.demo.model.Assignment;
import com.example.demo.model.StudentAssignment;
import com.example.demo.security.CustomUserDetails;
import com.example.demo.service.AssignmentService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/assignments")
@RequiredArgsConstructor
public class AssignmentController {
    private final AssignmentService assignmentService;

    // 获取学生作业列表
    @GetMapping("/course/{courseId}")
    @PreAuthorize("hasRole('STUDENT')")
    public ResponseEntity<List<Assignment>> getStudentAssignments(
            @PathVariable Long courseId,
            @AuthenticationPrincipal CustomUserDetails userDetails) {
        Long studentId = userDetails.getId();
        return ResponseEntity.ok(assignmentService.getStudentAssignments(courseId, studentId));
    }

    // 提交作业
    @PostMapping("/{assignmentId}/submit")
    @PreAuthorize("hasRole('STUDENT')")
    public ResponseEntity<?> submitAssignment(
            @PathVariable Long assignmentId,
            @RequestBody AssignmentSubmission submission,
            @AuthenticationPrincipal CustomUserDetails userDetails) {
        Long studentId = userDetails.getId();
        assignmentService.submitAssignment(studentId, assignmentId, submission);
        return ResponseEntity.ok().build();
    }

    // 获取作业提交详情
    @GetMapping("/{assignmentId}/submission")
    @PreAuthorize("hasRole('STUDENT')")
    public ResponseEntity<StudentAssignment> getSubmission(
            @PathVariable Long assignmentId,
            @AuthenticationPrincipal CustomUserDetails userDetails) {
        Long studentId = userDetails.getId();
        return ResponseEntity.ok(assignmentService.getStudentAssignment(studentId, assignmentId));
    }
}